{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ac9c7def",
   "metadata": {},
   "source": [
    "## 本周内容\n",
    "> 1. 有序的数据结构之列表\n",
    "> 2. 用户输入\n",
    "> 3. 关于格式化文本信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "363f397a",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_01_name = \"Marry\"\n",
    "\n",
    "# 思考：如果想一个变量存放一个班同学的名字，该怎么存？\n",
    "# 思考1：处理数据会不会比较麻烦？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "40a9df7d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Marry,Alex,Jack\n"
     ]
    }
   ],
   "source": [
    "网三_name_list = \"Marry,Alex,Jack\"\n",
    "print(网三_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "180da584",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Alex'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思考2：处理数据会不会比较麻烦？\n",
    "网三_name_list[6:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "bd9783d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思考3：我想知道这个名单有多少个人名？\n",
    "len(网三_name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8f75f43",
   "metadata": {},
   "source": [
    "## 引入列表的概念\n",
    "> 注释:我们已经学习了 数值 和 字符，list可以放数值和字符，但是通常要么一起放数值，要么一起放字符串。\n",
    "\n",
    "> 总结一些通用的有序数据结构的方法：\n",
    ">> 1. 取值：通过索引值取出对应的内容\n",
    ">> 2. 切片： 通过索引值区间取出对应的一些内容\n",
    ">> 3. in可以查看成员对象是否存在\n",
    "\n",
    "> 列表的一些方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d053e114",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4]\n",
    "print(a,type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3c4692dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Marry', 'Alex', 'Jack'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "网三_name_list = [\"Marry\",\"Alex\",\"Jack\"]\n",
    "print(网三_name_list,type(网三_name_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "984e7fe1",
   "metadata": {},
   "source": [
    "### 1.1列表的运算法则\n",
    "* 列表的数据本身不做修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "cfe4a4f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Marry'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取出 网三_name_list 中第一位同学的名字\n",
    "网三_name_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "00408cfa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看 网三_name_list 中有多少位同学\n",
    "len(网三_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "58325bcd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "网三： Marry\n",
      "网三： Alex\n",
      "网三： Jack\n"
     ]
    }
   ],
   "source": [
    "for i in 网三_name_list:\n",
    "    print(\"网三：\",i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "64d6637f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# in 可以查询数据是否存在\n",
    "\"Jack\" in 网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1d28bb97",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 关于运算法则(in + 切片 取值 len(s) s.count)\n",
    "网三_name_list_转专业 = [\"Xiaoming\",\"Lihua\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "809251c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', 'Jack', 'Xiaoming', 'Lihua']"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list + 网三_name_list_转专业"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "5bc0d748",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 思考：一个 list 可不可以出现重复项？\n",
    "网一_age_list = [18,19,19,18,20,19]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "cd672b3d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 19, 18, 20, 19]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_age_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8be9d141",
   "metadata": {},
   "source": [
    "### 2.2 列表的方法--对列表数据进行操作\n",
    "> 1. 增\n",
    "> 2. 删\n",
    "> 3. 改\n",
    "> 4. 查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d882d50e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', 'Jack']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "971639a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.append(\"张志豪\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "0b54d304",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', 'Jack', '张志豪']"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "1ab41b51",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.append(\"王一鸣\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "30d9be98",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', 'Jack', '张志豪', '王一鸣']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "14d32cdb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# insert 增加元素到某个位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "97bd16d5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网三_name_list.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "57aa1b55",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.insert(1,\"李华\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "dd1f70a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', 'Alex', 'Jack', '张志豪', '王一鸣']"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "c0ebb3c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# remove"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "f9d5e849",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网三_name_list.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "2767caad",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.remove(\"Alex\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "c615893a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', 'Jack', '张志豪', '王一鸣']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "2ef6206f",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "list.remove(x): x not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[1;32mIn [28]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43m网三_name_list\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mremove\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mAlex\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list"
     ]
    }
   ],
   "source": [
    "网三_name_list.remove('Alex')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "b702e948",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', 'Jack', '张志豪', '王一鸣']"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# remove 经常和 in 一起使用，先检查成员变量是否存在，然后再remove\n",
    "if \"Alex\" in 网三_name_list:\n",
    "    网三_name_list.remove(\"Alex\")\n",
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "d1f29936",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "c117e11f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网三_name_list.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "9a929d96",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', 'Jack', '张志豪', '王一鸣']"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "07f6936a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'王一鸣'"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "d2253d54",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Jack'"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list.pop(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "e4672e5e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', '张志豪']"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "d2ccb22a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践：请移除19的元素\n",
    "age_list = [18,19,18,19,17,20,20,18,19,16,17,20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "57e679d7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list.count(19)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "1e45accb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 17, 20, 20, 18, 16, 17, 20]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "a0abe8f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 17, 20, 20, 18, 16, 17, 20]"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思路1：\n",
    "删除的数量 = age_list.count(19)\n",
    "for i in range(删除的数量):\n",
    "    if 19 in age_list:\n",
    "        age_list.remove(19)\n",
    "    age_list\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "266901f4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 17, 20, 20, 18, 16, 17, 20]"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思路2：pop(index)，先决条件，知道值出现的位置\n",
    "for i,v in enumerate(age_list):\n",
    "    if v == 19:\n",
    "        print(i,v)\n",
    "        age_list.pop(i)\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65a04cd8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 课后练习：\n",
    "课本P68和P81"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a420584a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0fa7e9c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用一组列表方法转换和处理对象列表\n",
    "for i in range(4):\n",
    "    plist.pop()\n",
    "plist.pop(0)\n",
    "plist.remove(\"'\")\n",
    "plist.extend([plist.pop(),plist.pop()])\n",
    "plist.insert(2,plist.pop(3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a4098abd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "new_phrase = ''.join(plist)\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f3eb6236",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Ford Prefect', 'Male', 'Researcher', 'Betelgeuse Seven']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> person1 = ['Ford Prefect','Male','Researcher','Betelgeuse Seven']\n",
    ">>> person1\n",
    "['Ford Prefect','Male','Researcher','Betelgeuse Seven']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5804a1a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Name',\n",
       " 'Ford Prefect',\n",
       " 'Gender',\n",
       " 'Male',\n",
       " 'Occupation',\n",
       " 'Researcher',\n",
       " 'Home Planet',\n",
       " 'Betelgeuse Seven']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> person2 = ['Name','Ford Prefect','Gender','Male','Occupation','Researcher','Home Planet','Betelgeuse Seven']\n",
    ">>> person2\n",
    "['Name','Ford Prefect','Gender','Male','Occupation','Researcher','Home Planet','Betelgeuse Seven']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fd828423",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f82da406",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
